<?php

/*
 Open the connection to the mySQL database
*/

$conn = mysql_connect('studentdb.gl.umbc.edu', 'chrimo1', 'chrimo1');
if (!$conn) {
   die('Could not connect');
}

$db_selected = mysql_select_db('chrimo1', $conn);
if (!$db_selected) {
   die ('Can\'t use chrimo1');
}



$sql = "SELECT * FROM gameInventory";
$result = mysql_query($sql, $conn);
if (mysql_num_rows($result) == 0 ) {
   echo '<center><h2>There is no Inventory</h2></center>';
   echo '<center><a href="http://userpages.umbc.edu/~sehoove1/CMSC345/admin.html">BACK</a></center>';
   die();
}


/*
 If add to cart was selected, get the item information from the inventory table and add the item
 to the cart table with the username of the user who added it from the cookie
*/
if(isset($_GET['addId'])){

	$id = $_GET['addId'];

	$sql = "SELECT * FROM gameInventory WHERE idNum=".$id;


	$result = mysql_query($sql, $conn);



	if(mysql_num_rows($result) == 0){	
		echo 'Game &nbsp Not &nbsp Found.';
		die();
	}

	$r = mysql_fetch_array($result);

	$cost = $r['price'];
	$id = $r['idNum'];
	$quantity = $r['quantity'];
	echo "<br>Quantity:".$quantity;
	
	$sqlCart = "SELECT * FROM shoppingCart WHERE custIdNum='".$_COOKIE['user']."' AND gameId=".$id;
	$cartResult = mysql_query($sqlCart, $conn);

	if($quantity >= 1 && mysql_num_rows($cartResult) < ($quantity)){
		$sql1 = "INSERT INTO shoppingCart (  custIdNum ,  gameId ,  quantity,  cost ) VALUES ('".$_COOKIE['user']."',  '".$id."',  '1',  '".$cost."')";	
		if($insert = mysql_query($sql1, $conn)){
			$quantity = $quantity-1;

			if( $update = mysql_query($sql2, $conn)){
				
			}else{
				
			}
		}
		else{
			
		}
	
	
	}else{
		echo "<br>Sold Out.";
	}
}
/*
 If remove from cart is selected, remove one instance of the item from the cart
*/
if(isset($_GET['removeId'])){

	$id = $_GET['removeId'];

	$sql = "SELECT * FROM gameInventory WHERE idNum=".$id;


	$result = mysql_query($sql, $conn);



	if(mysql_num_rows($result) == 0){
		echo 'Game &nbsp Not &nbsp Found.';
		die();
	}

	$r = mysql_fetch_array($result);

	$cost = $r['price'];
	$id = $r['idNum'];
	$quantity = $r['quantity'];
	if($quantity >= 0){
		echo "<br>Game ID:".$id;
		$sql1 = "DELETE FROM shoppingCart WHERE custIdNum='".$_COOKIE['user']."' AND gameId=".$id." LIMIT 1";
		if($remove = mysql_query($sql1, $conn)){
			$quantity = $quantity+1;
			echo "<br>Updated quantity:".$quantity;

			if( $update = mysql_query($sql2, $conn)){
				
			}else{
	
			}
		}
		else{

		}
		

	}else{
		
	}

}

?>



<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<!-- InstanceBeginEditable name="doctitle" -->
<title>Legacy-Gamer</title>
<!-- InstanceEndEditable -->

<link rel="stylesheet" type="text/css" href="legacyGamer.css">

</head>
<body class = "body">
<div id="Header" class="style1">
	<div id="sublayer1" >
		<a href="index.php" style="float:left;"><img src="images/Title.png" height="115px" alt="store logo"></a>

		<a href="cart.php" style="float:right; height:115px; border-left:1px solid #ccc; position:relative;">
			<img src="images/cart.png" alt="store logo">
			<?php
				/*
				 Identify the number of items in the cart
				*/
				$sql = "SELECT * FROM shoppingCart WHERE custIdNum='".$_COOKIE['user']."'";
				$result = mysql_query($sql, $conn);
				echo '<h2 style="position: absolute; top:10px">&nbsp &nbsp'.mysql_num_rows($result).'</h2>';

			?>
		</a>
		<?php
			echo '<div style="font-size:10pt; color:#FFC80E; float:right;" align="right">Welcome, '.$_COOKIE["user"].'<br><a href="login.php?logout=1" >Logout</a></div>';
		?>
		
		
	</div>	
</div>
	
<div id="Layer2" class="style2" align="left" >
	<ul id="nav">
	  <h3 style="height:10px; color:#FFC80E;">Genre:</h3>
	  <li2><a href="listItems.php?id=BeatemUp">
 		Beat'em Up
	  </a></li2>
	  <li2><a href="listItems.php?id=RPG">
 		RPG
	  </a></li2>
	  <li2><a href="listItems.php?id=Platform">
 		Platform
	  </a></li2>
	  <li2><a href="listItems.php?id=Action">
 		Action
	  </a></li2>
	  <li2><a href="listItems.php?id=PuzzleStrategy">
 		Puzzle/Strategy
	  </a></li2>
	  <li2><a href="listItems.php?id=ShootemUp">
 		Shoot'em Up
	  </a></li2>
	  <li2><a href="listItems.php?id=CityBuildingSimulator">
 		City Building Simulator
	  </a></li2>
	  <li2><a href="listItems.php?id=ScrollingShooter">
 		Scrollling Shooter
	  </a></li2>
	  <li2><a href="listItems.php?id=Racing">
 		Racing
	  </a></li2>
	  <li2><a href="listItems.php?id=Sports">
 		Sports
	  </a></li2>
	  
	  <h3 style="height:10px; color:#FFC80E;">System:</h3>
	  <li2><a href="listItems.php?id=Sega">
 		Sega Genesis
	  </a></li2>
	  <li2><a href="listItems.php?id=SuperNintendo">
 		Super Nintendo
	  </a></li2>

	  <h3 style="height:10px; color:#FFC80E;">Player Modes:</h3>
	  <li2><a href="listItems.php?id=Single">
 		Single-Player
	  </a></li2>
	  <li2><a href="listItems.php?id=Multi">
 		MultiPlayer
	  </a></li2>
	  <li2><a href="listItems.php?id=Coop">
 		Cooperative
	  </a></li2>

	</ul>
</div>

<div id="Layer3" style="margin-left:5px; margin-top:0px; position:absolute; height:625px; z-index:1; left:175px; top:115px; overflow:inheret; max-width:89%; min-width:70%" align="left"> 
  <form method="get" action="search.php">
    <input style="width:72%;" type="text" name="search">  
    <input type="submit" value="Search">
    
  </form>
  <h1 style="align:center; color:#FFC80E;">Your Cart:<br></h1>
  <?php
	
	/*
	 Create a query to get all of the items in the shopping cart added by the current user
	 Post a message that the cart is empty if the query returns no results
	*/
	$sql = "SELECT * FROM shoppingCart WHERE custIdNum='".$_COOKIE['user']."'";
 	$cartResult = mysql_query($sql, $conn);
	if (mysql_num_rows($cartResult)==0){
 	  echo '<center><h2 style="color:#FFC80E;">There are no games in the cart.</h2></center>';
	  die();
	}
	
	/*
	 Post all of the items the user has added to the cart in a table
	*/
	
	echo '<table border="0"  style="color:#FFC80E;" >';
	echo '<tr>';
	echo '<th></th><th>Game Name</th><th>Console</th><th align="center">Release Date</th><th align="center">Price</th><th align="center">Quantity<br>In Stock</th><th min-width="100px" style="min-width:100px;"></th><th></th>';
	echo '</tr>';
	
	$netTotal = 0;

	while($cr = mysql_fetch_array($cartResult))
   	{
	  $gameID = $cr['gameId'];
	  $sql = "SELECT * FROM gameInventory WHERE idNum=".$gameID;
         $result = mysql_query($sql, $conn);
	  $r = mysql_fetch_array($result);

	  $idNum = $r['idNum'];
         $gameName = $r['gameName'];
         $developer = $r['developer'];
         $releaseDate = $r['releaseDate'];
         $qty = $r['quantity'];
         $price = $r['price'];
	  $netTotal = $netTotal + $price;
	  $image = $r['imageURL'];
	  if($r['system'] == snes){
	    $console = "Super Nintendo";
	  }
	  else{
	    $console = "Sega Genesis";
	  }
	  
         echo '<tr>';
	  echo '<td ><img src="'; echo $image; echo '" height="100px" ></td>';
         echo '<td style="color:#FFC80E;"><a style="color:#FFC80E;" href="showDetails.php?id=';
	  echo $idNum; 
	  echo '">'; 
	  echo $gameName; 
	  echo '</td></a>';
         echo '<td style="color:#FFC80E;">';echo $console;echo '</td>';
         echo '<td align="center" style="color:#FFC80E;">';echo $releaseDate;echo '</td>';
         echo '<td align="center" style="color:#FFC80E;">$'; echo $price; echo ' </td>';
	  echo '<td align="right">'.$qty.'</td>';
	  echo '<td align="right" height="35px" width="100px"><a href="cart.php?removeId='; echo $idNum; echo'" height="31px" width="91px" class="button">Remove</a></td>';
	  echo '<td>&nbsp; &nbsp;</td>';
         echo '</tr>';
       }

	/*
	 Calculate and post the total prices with tax
	*/
	$unformatedTax = $netTotal * .06;
	$tax = money_format('%i', $unformatedTax);
	$unformatedTotalPrice = $tax + $netTotal;
	$totalPrice = money_format('%i', $unformatedTotalPrice);
	
	echo '<tr>
	<td></td>
	<td></td>
	<td></td>
	<td></td>
	<td>Price:</td>
	<td></td>
	<td>$'.money_format('%i',$netTotal).'</td>
	</tr>
	<tr>
	<td></td>
	<td></td>
	<td></td>
	<td></td>
	<td>Tax:</td>
	<td></td>
	<td>$'.$tax.'</td>
	</tr>
	<tr>
	<td></td>
	<td></td>
	<td></td>
	<td></td>
	<td><br><br>Total Price:</td>
	<td></td>
	<td><br><br>$'.$totalPrice.'</td>
	</tr>
	/*
	 Post a link to the checkout page
	*/
	<td></td>
	<td></td>
	<td></td>
	<td></td>
	<td></td>
	<td align="right" height="35px" width="100px"><br><a href="checkout.php" height="31px" width="91px" class="button">Checkout</a></td>
	<td></td>
	</tr>';
	echo '</table>'

    ?>    

  
</div>

</body>



<body class="style2">
</body>
<!-- InstanceEnd --></html>
